# -*- coding: utf-8 -*-
"""Setup the lifepyscle application"""

import logging

import transaction
from tg import config

from lifepyscle.config.environment import load_environment

__all__ = ['setup_app']

log = logging.getLogger(__name__)


def setup_app(command, conf, vars):
    """Place any commands to setup lifepyscle here"""
    load_environment(conf.global_conf, conf.local_conf)
    # Load the models
    from lifepyscle import model
    from lifepyscle.model import *
    from lifepyscle.model import DBSession
    # load ean13 from the lib
    from lifepyscle.lib import ean13 as barcode
    bcode = barcode
    print "Creating tables"
    model.metadata.create_all(bind=config['pylons.app_globals'].sa_engine)

    manager = model.User()
    manager.user_name = u'manager'
    manager.display_name = u'Example manager'
    manager.email_address = u'manager@somedomain.com'
    manager.password = u'managepass'

    bart = model.User()
    bart.user_name = u'bart'
    bart.display_name = u'Bart Simpson'
    bart.email_address = u'bart@example.com'
    bart.password = u'cowabunga'
    bart_group = model.Group()
    bart_group.group_name = u'bart'
    bart_group.display_name = u'Barts Private Group'

    depp = model.User()
    depp.user_name = u'depp'
    depp.display_name = u'Unprivileged Depp'
    depp.email_address = u'unprivileged_depp@somedomain.com'
    depp.password = u'12345678'
    depp_group = model.Group()
    depp_group.group_name = u'depp'
    depp_group.display_name = u'Depps Private Group'

    DBSession.add(depp)
    depp.groups.append(depp_group)
    DBSession.add(bart)
    bart.groups.append(bart_group)

    DBSession.add(manager)

    group = model.Group()
    group.group_name = u'managers'
    group.display_name = u'Managers Group'

    adming = model.Group()
    adming.group_name = u'admin'
    adming.display_name = u'Administrators'
    
    adming.users.append(bart)

    group.users.append(manager)
    group.users.append(bart)
    group.users.append(depp)

    DBSession.add(adming)
    DBSession.add(group)

    permission = model.Permission()
    permission.permission_name = u'manage'
    permission.description = u'This permission give a manage right to the bearer'

    admin_permission = model.Permission()
    admin_permission.permission_name = u'admin'
    admin_permission.description = u'This permission gives an administrative right'

    admin_permission.groups.append(adming)
    permission.groups.append(group)

    DBSession.add(permission)

    editor = model.User()
    editor.user_name = u'editor'
    editor.display_name = u'Example editor'
    editor.email_address = u'editor@somedomain.com'
    editor.password = u'editpass'

    DBSession.add(editor)

    R1,R2,R3,R4 = Record(),Record(),Record(),Record()
    R1.ean13 = bcode.ean13()
    R2.ean13 = bcode.ean13()
    R3.ean13 = bcode.ean13()
    R4.ean13 = bcode.ean13()
    P1,P2,P3,P4 = Party(),Party(),Party(),Party()
    P1.name,P1.nick = u'Big Bad Corporation',u'Big Bad'
    P2.name,P2.nick = u'Little Bad Corporation',u'Little Bad'
    P3.name,P3.nick = u'Angel, Inc',u'Angel'
    P4.name,P4.nick = u'Dragon Corporation',u'Dragon'
    R1.title = u'Framework Agreement to provide cheese'
    R1.description = u'Provide 10kg of swiss cheese'
    R2.title = u'Framework Agreement to provide carrots'
    R2.description =u'Provide 400 tonnes of carrots'
    R3.title = u'Framework Agreement to provide bananas'
    R3.description = u'Provide 50 bananas'
    R4.title = u'Framework Agreement to provide peanuts'
    R4.description = u'Provide 5 million tonnes of peanuts'
    P2.parent = P1
    R1.parties.append(P1)
    R1.parties.append(P4)
    R2.parties.append(P1)
    R2.parties.append(P2)
    R3.parties.append(P4)
    R3.parties.append(P3)
    R4.parties.append(P3)
    R4.parties.append(P1)
    bart.records.append(R1)
    bart.records.append(R2)
    bart.records.append(R3)
    bart.records.append(R4)
    R4.users_read.append(depp)

    transaction.commit()
    print "Successfully setup"
